package ServletDemo;
/**
 * 控制用户访问时间
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeControl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8"); //设置响应的 MIME类型。
        //获得一个向客户发送数据的输出流：
        PrintWriter out = resp.getWriter();
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("Now Time:");
        Date date = new Date();
        out.println("<BR>" + date);
//        System.out.println("时间是-->"+date);
        //Sun Oct 13 20:32:19 CST 2019
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH");
//        System.out.println("dateFormat.format(date)->"+dateFormat.format(date));
//        f.format()将日期格式化后是String类型，
//        f.format(new Date())
//        a.before(b)  //表示a小于b
//        2、用parse方法解析字符串，
//        f.parse(f.format(new Date()))
//        3、然后利用before进行比较。
        String s = dateFormat.format(date);
        int a = Integer.parseInt(s);
        System.out.println(a);
        if (a < 22 || a > 6) {

        } else {
            resp.setHeader("refresh", "0.1;http://localhost:8080/javaWeb_war_exploded/ServletDemo/StopVisit");//设置 Refresh 的值。0.1秒刷新页面一次,
        }
        out.println("</BODY>");
        out.println("</HTML>");
    }
}
